function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}

function getShowID(){
    var m = window.location.href.match(/show\/(\d+)/);
    if( m != null ){
        return m[1];
    }
    return null;
}

function disable_profile_selection()
{
    var profile = getQueryString('profile');
    if( profile != null ){
        $('#profile option[value!="'+profile+'"]').remove();

		var s = $('#profile');
		s.val(profile); // Select the option with a value of '1'
		s.trigger('change'); // Notify any JS components that the value changed
    }
}

function disable_unit_selection()
{
    var unit = getQueryString('unit');
    if( unit != null ){
        $('#unit option[value!="'+unit+'"]').remove();

		var s = $('#unit');
		s.val(unit); // Select the option with a value of '1'
		s.trigger('change'); // Notify any JS components that the value changed
    }
}

function disable_selector_selection()
{
    var selector = getQueryString('selector');
    if( selector != null ){
        $('#selector option[value!="'+selector+'"]').remove();

		var s = $('#selector');
		s.val(selector); // Select the option with a value of '1'
		s.trigger('change'); // Notify any JS components that the value changed
    }
}


function html_actions(conf)
{
    lines = [];
    lines.push('<center>');
    lines.push('<div class="btn-group btn-group-xs" style="display: flex;">');

    if( conf.edit ){
        lines.push( [
            '<a onclick="', conf.edit.onclick,
                '" class="btn btn-sm btn-default">',
                '<i class="fa fa-edit"></i>',
            '</a>'].join(''));
    }
    if( conf.remove ){
        lines.push([
            '<a data-text="',
            conf.remove.text,
            '" data-href="',
            conf.remove.link,
            '" class="btn btn-sm btn-default confirm" data-original-title="',
            conf.remove.title,
            '" onclick="remove_item(this);">',
                '<i class="fa fa-eraser"></i>',
            '</a>'
        ].join(''))
    }
    lines.push('</div>');
    lines.push('</center>');

    return lines.join("\n");
}

function remove_item(e)
{
    console.log(e);
    $('#modal-confirm .modal-title').text( e.getAttribute('data-original-title') );
    $('#modal-confirm .modal-text').text( e.getAttribute('data-text') );


    var m =$('#modal-confirm');
    m.on('shown.bs.modal', function () {
        console.log('somethine')
        $('#modal-confirm-ok').attr('href', e.getAttribute('data-href'));
    });
    m.modal();
}


function add_shortcuts(conf)
{
/*
<button class="btn  btn-sm" style="float: right;">Save<i class="fa fa-file-text-o"></i></button>
*/
    //add link for tray
    var link = '<a class="btn  btn-sm btn-default" style="float: right;" href="link">New&nbsp;<i class="fa fa-file-text-o"></i></a>';
    for(key in conf){
        $('#'+key).parent().prepend(link.replace("link", conf[key]));
    }

}